package com.yaoxiaoqing.objectclass;

import java.util.Arrays;

/*
 * 关于变量的赋值
 *
 * 	如果变量是基本数据类型，此时赋值的是变量所保存的数据值。
 * 	如果变量是引用数据类型，此时赋值的是变量所保存的数据的地址值。
 *
 */
public class ValueTransfer {
    public static void main(String[] args) {

        BaseType();

        BaseType2();

        BaseType3();
    }
    public static void BaseType(){
        System.out.println("**********基本数据类型：***********");
        int m = 10;
        int n = m;
        System.out.println("m = " + m + ", n = " + n);//m = 10, n = 10
        n = 20;
        System.out.println("m = " + m + ", n = " + n);//m = 10, n = 20 说明m,n直接传递的值

        System.out.println("***********引用数据类型:********");
        Order o1 = new Order();
        o1.orderId = 1001;

        Order o2 = o1;//指向同一个引用地址

        System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " + o2.orderId);

        o2.orderId = 1002;
        System.out.println("o1.orderId = " + o1.orderId + ",o2.orderId = " + o2.orderId);

    }

    public static void BaseType2() {
        System.out.println("*****BAseTyep2******");
        int m = 10;
        int n = 20;

        System.out.println("m = " + m + ", n = " + n);//m = 10, n = 20
        //交换两个变量的值的操作
//		int temp = m;
//		m = n;
//		n = temp;    //m = 20, n = 10  这种情况是直接给m，n的地址赋值

        ValueTransfer test = new ValueTransfer();
        test.swap(m, n);//这种是把值 传进去了

        System.out.println("m = " + m + ", n = " + n);//m = 10, n = 20
    }

    public static void BaseType3() {
        System.out.println("****BaseTyep3*****");
        int num = 50;
        int[] arr = {1, 2, 3};

        updateValue(num);
        System.out.println(num); // 输出 50，因为对参数的修改不会影响原始变量

        updateArray(arr);
        System.out.println(Arrays.toString(arr)); // 输出 [1, 2, 4]
    }
    public static void updateValue(int value) {
        value = 100;
    }

    public static void updateArray(int[] array) {
        array[2] = 4;
    }
    public void swap(int m,int n){
        int temp = m;
        m = n;
        n = temp;
    }
}
class  Order{
    int orderId;
}
